# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# Contributor: wirx6 <wirx654@gmail.com>

_realname=Vulkan-Loader
pkgbase=mingw-w64-vulkan-loader
pkgname=("${MINGW_PACKAGE_PREFIX}-vulkan-loader")
provides=("${MINGW_PACKAGE_PREFIX}-vulkan")
replaces=("${MINGW_PACKAGE_PREFIX}-vulkan")
pkgver=1.2.166
pkgrel=1
pkgdesc='Vulkan Installable Client Driver (ICD) Loader (mingw-w64)'
arch=('any')
url="https://www.khronos.org/vulkan/"
groups=("${MINGW_PACKAGE_PREFIX}-vulkan-devel")
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-vulkan-headers")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/KhronosGroup/${_realname}/archive/v${pkgver}.tar.gz"
        002-proper-def-files-for-32bit.patch
        003-generate-pkgconfig-files.patch
        004-disable-suffix-in-static-lib.patch)
sha256sums=('1094b303ead1843fd31a5c11b0f5c2f91949b0608f36619bf92c738f6d561b35'
            'e6712796c69d88172b38ae467ff27e9d40101b8934d52f944a49898493378039'
            'd97fbcc042eb303812df7bc5f714ffacaf36381a77ced0991004df5dac9e975b'
            '4924977e780c45e970e2a7311ea67ab2b208ca487c584be8702721ce060d5d71')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  #patch -p1 -i ${srcdir}/001-build-fix.patch
  patch -p1 -i ${srcdir}/002-proper-def-files-for-32bit.patch
  patch -p1 -i ${srcdir}/003-generate-pkgconfig-files.patch
  patch -p1 -i ${srcdir}/004-disable-suffix-in-static-lib.patch
}

build() {
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -D__STDC_FORMAT_MACROS -Wno-unused-function" \
  CPPFLAGS+=" -D__STDC_FORMAT_MACROS" \
  CXXFLAGS+=" -D__USE_MINGW_ANSI_STDIO -D__STDC_FORMAT_MACROS -fpermissive -Wno-unused-function" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    "${extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DUSE_MASM=OFF \
    -DBUILD_TESTS=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd ${srcdir}/build-${CARCH}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  if [ "${CARCH}" = "i686" ]; then
    rm -f ${pkgdir}${MINGW_PREFIX}/lib/libvulkan.dll.a
    ${MINGW_PREFIX}/bin/gendef ${pkgdir}${MINGW_PREFIX}/bin/libvulkan-1.dll
    ${MINGW_PREFIX}/bin/dlltool -k -d libvulkan-1.def -l ${pkgdir}${MINGW_PREFIX}/lib/libvulkan.dll.a
  fi

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.TXT ${pkgdir}${MINGW_PREFIX}/share/licenses/vulkan-loader/LICENSE
}
